﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit;
using YArchitech.Revit.Electric;
using YArchitech.Revit.Electric.Common;
using HYElectricKernel;
using HYElectricSetting;

namespace HYRElectric.WirePlace
{
	internal class NewConnectWire
	{
		public static void CreateWireBySelection(ExternalCommandData cmdData, UIDocument uiDoc)
		{
			Document document = uiDoc.Document;
			Selection selection = uiDoc.Selection;
			WireType wType = null;
			bool flag = true;
			Element element = null;
			Element element2 = null;
			try
			{
				for (;;)
				{
					if (flag)
					{
						try
						{
							NewConnectWire.isFirstEquipment = false;
							Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new SelectionFilter(), "请选择要连线的第一个设备");
							element = document.GetElement(reference);
						}
						catch (Exception)
						{
							NewConnectWire.isFirstEquipment = true;
							throw;
						}
						Reference reference2 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new SelectionFilter(), "请选择要连线的设备");
						element2 = document.GetElement(reference2);
						flag = false;
					}
					else
					{
						element = element2;
						Reference reference3 = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new SelectionFilter(), "请选择要连线的设备");
						element2 = document.GetElement(reference3);
					}
					if (element.Id == element2.Id)
					{
						TaskDialog.Show("盈建科提示", "两次选择的为同一对象，请重新选择。");
					}
					else
					{
						foreach (object obj in document.Settings.ElectricalSetting.WireTypes)
						{
							WireType wireType = (WireType)obj;
							if (wireType.Name.Equals(WireConnectForm.wireParams.WireType))
							{
								wType = wireType;
								break;
							}
						}
						RevitUtils.GetDeviceConnector(element);
						RevitUtils.GetDeviceConnector(element2);
						EquipmentEdgeLine equipmentEdgeLine = new EquipmentEdgeLine(document, element, element2, WireConnectForm.wireParams);
						EdgeInfo edgeInfo = null;
						equipmentEdgeLine.GetDeviceEdgePoints(ref edgeInfo, null);
						if (edgeInfo == null)
						{
							break;
						}
						switch (WireConnectForm.wireParams.WireConnectType)
						{
						case EnumWireConnectType.up:
						{
							List<Element> crossElement = NewConnectWire.GetCrossElement(edgeInfo.StartPoint, edgeInfo.EndPoint, element.Id, element2.Id, equipmentEdgeLine, document);
							crossElement.Add(element);
							crossElement.Add(element2);
							EquipmentConnectWires.CreatWireByElementList(crossElement, document);
							break;
						}
						case EnumWireConnectType.bottom:
						{
							List<Element> crossElement2 = NewConnectWire.GetCrossElement(edgeInfo.StartPoint, edgeInfo.EndPoint, element.Id, element2.Id, equipmentEdgeLine, document);
							List<XYZ> insectPoints = new List<XYZ>();
							if (crossElement2.Count > 0)
							{
								insectPoints = NewConnectWire.GetWireInsectionPoints(crossElement2, edgeInfo.StartPoint, edgeInfo.EndPoint, equipmentEdgeLine, document);
							}
							NewConnectWire.CreateWireBottom(document, element, element2, wType, edgeInfo, insectPoints);
							break;
						}
						case EnumWireConnectType.direct:
							NewConnectWire.CreateWireDirected(document, element, element2, wType, edgeInfo);
							break;
						default:
							NewConnectWire.CreateWireDirected(document, element, element2, wType, edgeInfo);
							break;
						}
					}
				}
				TaskDialog.Show("提示", "EdgeInfo信息为空");
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				throw;
			}
		}

		private static void CreateWireDirected(Document doc, Element startdevice, Element endDevice, WireType wType, EdgeInfo edge)
		{
			Connector deviceConnector = RevitUtils.GetDeviceConnector(startdevice);
			Connector deviceConnector2 = RevitUtils.GetDeviceConnector(endDevice);
			Transaction transaction = new Transaction(doc, "ConnectWire");
			transaction.Start();
			try
			{
				Wire wire = NewConnectWire.CreateWireByInfo(doc, wType, WireConnectForm.wireParams.WireStyle, edge, deviceConnector, deviceConnector2);
				NewConnectWire.AssignParameters(doc, wire);
				transaction.Commit();
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				string message = ex.Message;
				throw;
			}
		}

		private static void CreateWireBottom(Document doc, Element startdevice, Element endDevice, WireType wType, EdgeInfo edge, List<XYZ> InsectPoints)
		{
			Wire wire = null;
			Connector deviceConnector = RevitUtils.GetDeviceConnector(startdevice);
			Connector deviceConnector2 = RevitUtils.GetDeviceConnector(endDevice);
			List<XYZ> list = new List<XYZ>();
			list.Add(edge.StartPoint);
			if (InsectPoints.Count > 1)
			{
				list.InsertRange(1, InsectPoints);
			}
			list.Add(edge.EndPoint);
			Connector endConnectorTo = null;
			Connector connector = null;
			for (int i = 0; i < list.Count; i += 2)
			{
				Connector startConnectorTo;
				if (i == 0)
				{
					startConnectorTo = deviceConnector;
				}
				else
				{
					startConnectorTo = null;
				}
				if (list.Count - 1 == i + 1)
				{
					endConnectorTo = deviceConnector2;
				}
				XYZ xyz = list[i];
				XYZ xyz2 = list[i + 1];
				if (xyz.DistanceTo(xyz2) > 1.3123359580052494)
				{
					if (i != 0)
					{
						xyz += (xyz2 - xyz).Normalize() * 200.0 / 304.8;
					}
					if (list.Count - 1 != i + 1)
					{
						xyz2 += (xyz - xyz2).Normalize() * 200.0 / 304.8;
					}
				}
				Transaction transaction = new Transaction(doc, "ConnectWire");
				transaction.Start();
				try
				{
					List<XYZ> list2 = new List<XYZ>();
					list2.Add(xyz);
					list2.Add(xyz2);
                    wire = HyRevitUtils.CreateWire(doc, wType, doc.ActiveView, (Autodesk.Revit.DB.Electrical.WiringType)1, list2, startConnectorTo, endConnectorTo);
					ConnectorSet connectors = wire.ConnectorManager.Connectors;
					if (connector != null)
					{
						double num = double.MaxValue;
						Connector connector2 = null;
						XYZ origin = null;
						foreach (object obj in connectors)
						{
							Connector connector3 = (Connector)obj;
							if (connector3.Origin.DistanceTo(connector.Origin) < num)
							{
								num = connector3.Origin.DistanceTo(connector.Origin);
								connector2 = connector3;
								origin = connector3.Origin;
							}
						}
						connector2.ConnectTo(connector);
						connector2.Origin = origin;
					}
					foreach (object obj2 in connectors)
					{
						Connector connector4 = (Connector)obj2;
						if (!connector4.IsConnected)
						{
							connector = connector4;
						}
					}
					NewConnectWire.AssignParameters(doc, wire);
					transaction.Commit();
				}
				catch (Exception ex)
				{
					transaction.RollBack();
					string message = ex.Message;
					throw;
				}
			}
		}

		private static List<XYZ> GetWireInsectionPoints(List<Element> elementList, XYZ pts, XYZ pte, EquipmentEdgeLine edgeLine, Document doc)
		{
			Line line = Line.CreateBound(new XYZ(pts.X, pts.Y, 0.0), new XYZ(pte.X, pte.Y, 0.0));
			List<XYZ> list = new List<XYZ>();
			for (int i = 0; i < elementList.Count; i++)
			{
				List<Curve> deviceEdgeCurves = edgeLine.GetDeviceEdgeCurves(elementList[i]);
				edgeLine.MirroredCurves(elementList[i], ref deviceEdgeCurves);
				List<XYZ> list2 = new List<XYZ>();
				foreach (Curve curve in deviceEdgeCurves)
				{
					XYZ xyz;
					if (curve is Arc)
					{
						xyz = (curve as Arc).Center;
					}
					else
					{
						xyz = curve.GetEndPoint(0);
					}
					Transform transform = Transform.CreateTranslation(new XYZ(0.0, 0.0, 0.0 - xyz.Z));
					Curve curve2 = curve.CreateTransformed(transform);
					IntersectionResultArray intersectionResultArray;
					SetComparisonResult setComparisonResult = line.Intersect(curve2, out intersectionResultArray);
					if (setComparisonResult == SetComparisonResult.Overlap)
					{
						IEnumerator enumerator2 = intersectionResultArray.GetEnumerator();
						{
							while (enumerator2.MoveNext())
							{
								object obj = enumerator2.Current;
								IntersectionResult intersectionResult = (IntersectionResult)obj;
								XYZ item = new XYZ(intersectionResult.XYZPoint.X, intersectionResult.XYZPoint.Y, xyz.Z);
								list2.Add(item);
							}
							continue;
						}
					}
                    if (setComparisonResult == (SetComparisonResult)64)
					{
						list2.Add(curve.GetEndPoint(0));
						list2.Add(curve.GetEndPoint(1));
					}
				}
				if (list2.Count > 0)
				{
					list2.Sort(new PointComparer(pts));
					list.Add(list2[0]);
					list.Add(list2[list2.Count - 1]);
				}
			}
			if (list.Count > 1)
			{
				list.Sort(new PointComparer(pts));
			}
			return list;
		}

		private static List<Element> GetCrossElement(XYZ pts, XYZ pte, ElementId startDeviceId, ElementId endDeviceId, EquipmentEdgeLine edgeLine, Document doc)
		{
			List<Element> list = new List<Element>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc, doc.ActiveView.Id);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(FamilyInstance));
			List<Element> list2 = filteredElementCollector.WherePasses(elementClassFilter).OfCategory(BuiltInCategory.OST_LightingFixtures).ToElements().ToList<Element>();
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(doc, doc.ActiveView.Id);
			new ElementClassFilter(typeof(FamilyInstance));
			List<Element> collection = filteredElementCollector2.WherePasses(elementClassFilter).OfCategory(BuiltInCategory.OST_ElectricalFixtures).ToElements().ToList<Element>();
			list2.AddRange(collection);
			for (int i = 0; i < list2.Count; i++)
			{
				if (list2[i] is FamilyInstance)
				{
					ConnectorSetIterator connectorSetIterator = (list2[i] as FamilyInstance).MEPModel.ConnectorManager.Connectors.ForwardIterator();
					while (connectorSetIterator.MoveNext())
					{
                        if ((connectorSetIterator.Current as Connector).Domain != (Domain)2)
						{
							list2.RemoveAt(i);
							i--;
							break;
						}
					}
				}
			}
			Line line = Line.CreateBound(new XYZ(pts.X, pts.Y, 0.0), new XYZ(pte.X, pte.Y, 0.0));
			for (int j = 0; j < list2.Count; j++)
			{
				if (!list2[j].Id.Equals(startDeviceId) && !list2[j].Id.Equals(endDeviceId))
				{
					List<Curve> deviceEdgeCurves = edgeLine.GetDeviceEdgeCurves(list2[j]);
					edgeLine.MirroredCurves(list2[j], ref deviceEdgeCurves);
					foreach (Curve curve in deviceEdgeCurves)
					{
						XYZ xyz;
						if (curve is Arc)
						{
							xyz = (curve as Arc).Center;
						}
						else
						{
							xyz = curve.GetEndPoint(0);
						}
						Transform transform = Transform.CreateTranslation(new XYZ(0.0, 0.0, 0.0 - xyz.Z));
						Curve curve2 = curve.CreateTransformed(transform);
						IntersectionResultArray intersectionResultArray;
						SetComparisonResult setComparisonResult = line.Intersect(curve2, out intersectionResultArray);
						if (setComparisonResult == SetComparisonResult.Overlap)
						{
							list.Add(list2[j]);
							break;
						}
                        if (setComparisonResult == (SetComparisonResult)64)
						{
							list.Add(list2[j]);
							break;
						}
					}
				}
			}
			return list;
		}

		public static void CreateWireBySelected(Document doc, List<Element> devices, EdgeInfo edge)
		{
			WireType wType = null;
			try
			{
				foreach (object obj in doc.Settings.ElectricalSetting.WireTypes)
				{
					WireType wireType = (WireType)obj;
					if (wireType.Name.Equals(WireConnectForm.wireParams.WireType))
					{
						wType = wireType;
						break;
					}
				}
				Connector deviceConnector = RevitUtils.GetDeviceConnector(devices[0]);
				Connector deviceConnector2 = RevitUtils.GetDeviceConnector(devices[1]);
				Wire wire = NewConnectWire.CreateWireByInfo(doc, wType, WireConnectForm.wireParams.WireStyle, edge, deviceConnector, deviceConnector2);
				NewConnectWire.AssignParameters(doc, wire);
			}
			catch (Exception ex)
			{
				TaskDialog.Show("盈建科提示", ex.Message);
				throw;
			}
		}

		public static Wire CreateWire(Document doc, Element startdevice, Element endDevice, WiringType wiringType, XYZ wireCenter = null)
		{
			WireType wType = null;
			Wire result;
			try
			{
				WireConnectForm.wireParams.WireStyle = wiringType;
				if (NewConnectWire.IsHasFile())
				{
					NewConnectWire.LoadDlgData(doc);
				}
				NewConnectWire.CheckDefaultValue(doc);
				foreach (object obj in doc.Settings.ElectricalSetting.WireTypes)
				{
					WireType wireType = (WireType)obj;
					wType = wireType;
					if (wireType.Name.Equals(WireConnectForm.wireParams.WireType))
					{
						wType = wireType;
						break;
					}
				}
				Connector deviceConnector = RevitUtils.GetDeviceConnector(startdevice);
				Connector deviceConnector2 = RevitUtils.GetDeviceConnector(endDevice);
				if (NewConnectWire.To2d(deviceConnector.Origin).IsAlmostEqualTo(NewConnectWire.To2d(deviceConnector2.Origin)))
				{
					result = null;
				}
				else
				{
					EquipmentEdgeLine equipmentEdgeLine = new EquipmentEdgeLine(doc, startdevice, endDevice, WireConnectForm.wireParams);
					EdgeInfo edge = null;
					equipmentEdgeLine.GetDeviceEdgePoints(ref edge, wireCenter);
					Transaction transaction = new Transaction(doc, "ConnectWire");
					transaction.Start();
					Wire wire = NewConnectWire.CreateWireByInfo(doc, wType, wiringType, edge, deviceConnector, deviceConnector2);
					NewConnectWire.AssignParameters(doc, wire);
					transaction.Commit();
					result = wire;
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private static Wire CreateWireByInfo(Document doc, WireType wType, WiringType wiringType, EdgeInfo edge, Connector startdeviceCon, Connector endDeviceCon)
		{
			List<XYZ> list = new List<XYZ>();
			Wire result;
            if (wiringType == (WiringType)1)
			{
				list.Add(edge.StartPoint);
				list.Add(edge.EndPoint);
                result = HyRevitUtils.CreateWire(doc, wType, doc.ActiveView, (Autodesk.Revit.DB.Electrical.WiringType)1, list, startdeviceCon, endDeviceCon);
			}
			else
			{
				list.Add(edge.StartPoint);
				XYZ item = NewConnectWire.CalculatMidPoint(edge.ConvertTo() as Arc);
				list.Add(item);
				list.Add(edge.EndPoint);
                result = HyRevitUtils.CreateWire(doc, wType, doc.ActiveView, (Autodesk.Revit.DB.Electrical.WiringType)0, list, startdeviceCon, endDeviceCon);
			}
			return result;
		}

		public static XYZ CalculatMidPoint(Arc arc)
		{
			XYZ hyendPoint = arc.GetEndPoint(0);
			XYZ center = arc.Center;
			XYZ pt = (hyendPoint - center).Normalize();
			double radius = arc.Radius;
			double num = arc.Length / radius / 2.0;
			if (num > Math.PI*.5)
			{
				num -= Math.PI;
			}
			XYZ xyz = Geometry.RotateTo(pt, num, XYZ.BasisZ).Normalize();
			if (Geometry.IsEqual(arc.Normal.Z, -1.0))
			{
				xyz = Geometry.RotateTo(pt, -num, XYZ.BasisZ).Normalize();
			}
			return center + xyz * radius;
		}

		private static void CheckDefaultValue(Document doc)
		{
			if (string.IsNullOrEmpty(WireConnectForm.wireParams.WireType))
			{
				WireConnectForm.wireParams.WireType = RevitFilter.GetElementsOfType(doc, typeof(WireType)).ToList<Element>()[0].Name;
			}
			if (WireConnectForm.wireParams.WireNum == 0)
			{
				WireConnectForm.wireParams.WireNum = 1;
			}
			if (WireConnectForm.wireParams.CrossSection == 0.0)
			{
				WireConnectForm.wireParams.CrossSection = 2.5;
			}
			if (string.IsNullOrEmpty(WireConnectForm.wireParams.LayMode))
			{
				WireConnectForm.wireParams.WireType = "WE";
			}
			if (WireConnectForm.wireParams.Level == 0.0)
			{
				WireConnectForm.wireParams.Level = 3.0;
			}
			if (WireConnectForm.wireParams.ConduitTypeID == null)
			{
				IList<Element> list = RevitFilter.GetElementsOfType(doc, typeof(ConduitType)).ToList<Element>();
				WireConnectForm.wireParams.ConduitType = list[0].Name;
				WireConnectForm.wireParams.ConduitTypeID = list[0].Id;
			}
			if (WireConnectForm.wireParams.ConduitDiameter == 0.0)
			{
				WireConnectForm.wireParams.ConduitDiameter = 15.0;
			}
		}

		private static void RemoveDuplicateConnection(Document doc, Element ele1, Element ele2)
		{
			foreach (Element element in RevitFilter.GetElementsOfType(doc, doc.ActiveView.Id, typeof(Wire)).ToList<Element>())
			{
				Wire wire = element as Wire;
				Element element2;
				Element element3;
				RevitUtils.GetWireConnectedEquipment(wire, out element2, out element3);
				if (element2 != null && element3 != null && ((element2.Id == ele1.Id && element3.Id == ele2.Id) || (element2.Id == ele2.Id && element3.Id == ele1.Id)))
				{
					doc.Delete(wire.Id);
				}
			}
		}

		public static Arc GetArc(Document doc, XYZ p1, XYZ p2, bool isCircle)
		{
			XYZ arcMiddlePoint = NewConnectWire.GetArcMiddlePoint(doc, p1, p2);
			if (isCircle)
			{
				XYZ xyz = null;
				if (Geometry.TriangleCircumcentre(NewConnectWire.To2d(p1), NewConnectWire.To2d(p2), NewConnectWire.To2d(arcMiddlePoint), ref xyz))
				{
					Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin((p1 - xyz).CrossProduct(arcMiddlePoint - xyz), xyz);
					double radius = xyz.DistanceTo(NewConnectWire.To2d(arcMiddlePoint));
					return doc.CreatYJKArc(plane, radius, 0.0, 6.2657320146596431);
				}
			}
			return doc.CreatYJKArc(new XYZ(p1.X, p1.Y, 0.0), new XYZ(p2.X, p2.Y, 0.0), new XYZ(arcMiddlePoint.X, arcMiddlePoint.Y, 0.0));
		}

		public static Arc GetArcByCenter(Document doc, XYZ p1, XYZ p2, XYZ center)
		{
			XYZ xyz = p2 - p1;
			xyz = Geometry.RotateTo(xyz, Math.PI*.5, XYZ.BasisZ);
			XYZ xyz2 = Geometry.CalculatMidPoint(p1, p2);
			center = Geometry.CalculateFootPoint(xyz2, xyz2 + xyz, center);
			xyz2 = center + (xyz2 - center).Normalize() * p1.DistanceTo(center);
			return doc.CreatYJKArc(new XYZ(p1.X, p1.Y, 0.0), new XYZ(p2.X, p2.Y, 0.0), new XYZ(xyz2.X, xyz2.Y, 0.0));
		}

		public static XYZ GetArcMiddlePoint(Document doc, XYZ p1, XYZ p2)
		{
			XYZ xyz = NewConnectWire.To2d(p1);
			XYZ xyz2 = NewConnectWire.To2d(p2);
			double num = xyz.DistanceTo(xyz2);
			XYZ xyz3 = (xyz + xyz2) / 2.0;
			XYZ vect = (xyz - xyz3).Normalize();
			double angle = NewConnectWire.GetAngle(xyz, xyz2);
			XYZ xyz4;
			if ((angle >= 0.0 && angle < Math.PI*.5) || (angle >= 4.71238898038469 && angle < Math.PI*2))
			{
				xyz4 = NewConnectWire.Rotate(vect, new XYZ(0.0, 0.0, 1.0), Math.PI*.5);
			}
			else
			{
				xyz4 = NewConnectWire.Rotate(vect, new XYZ(0.0, 0.0, 1.0), -Math.PI*.5);
			}
			return xyz3 + xyz4.Normalize() * (num / 6.0);
		}

		private static XYZ To2d(XYZ pt)
		{
			return new XYZ(pt.X, pt.Y, 0.0);
		}

		private static double GetAngle(XYZ ptCenter, XYZ ptOut)
		{
			XYZ basisX = XYZ.BasisX;
			XYZ basisZ = XYZ.BasisZ;
			XYZ xyz = ptOut - ptCenter;
			return NewConnectWire.FormatAngle(XYZ.BasisX.AngleOnPlaneTo(xyz, XYZ.BasisZ));
		}

		private static double FormatAngle(double angle)
		{
			int num = (int)(angle / Math.PI*2);
			double num2 = angle - (double)num * Math.PI*2;
			if (num2 < 0.0)
			{
				num2 += Math.PI*2;
			}
			return num2;
		}

		public static XYZ Rotate(XYZ vect, XYZ axis, double angle)
		{
			double x = axis.X;
			double y = axis.Y;
			double z = axis.Z;
			double[,] array = new double[3, 3];
			array[0, 0] = x * x;
			array[0, 1] = x * y;
			array[0, 2] = x * z;
			array[1, 0] = y * x;
			array[1, 1] = y * y;
			array[1, 2] = y * z;
			array[2, 0] = z * x;
			array[2, 1] = z * y;
			array[2, 2] = z * z;
			double[,] array2 = array;
			double[,] array3 = new double[3, 3];
			array3[0, 1] = -z;
			array3[0, 2] = y;
			array3[1, 0] = z;
			array3[1, 2] = -x;
			array3[2, 0] = -y;
			array3[2, 1] = x;
			double[,] array4 = array3;
			double[,] array5 = new double[3, 3];
			array5[0, 0] = 1.0 - array2[0, 0];
			array5[0, 1] = -array2[0, 1];
			array5[0, 2] = -array2[0, 2];
			array5[1, 0] = -array2[1, 0];
			array5[1, 1] = 1.0 - array2[1, 1];
			array5[1, 2] = -array2[1, 2];
			array5[2, 0] = -array2[2, 0];
			array5[2, 1] = -array2[2, 1];
			array5[2, 2] = 1.0 - array2[2, 2];
			double[,] array6 = array5;
			double[,] array7 = new double[3, 3];
			array7[0, 0] = array2[0, 0] + Math.Cos(angle) * array6[0, 0] + Math.Sin(angle) * array4[0, 0];
			array7[0, 1] = array2[0, 1] + Math.Cos(angle) * array6[0, 1] + Math.Sin(angle) * array4[0, 1];
			array7[0, 2] = array2[0, 2] + Math.Cos(angle) * array6[0, 2] + Math.Sin(angle) * array4[0, 2];
			array7[1, 0] = array2[1, 0] + Math.Cos(angle) * array6[1, 0] + Math.Sin(angle) * array4[1, 0];
			array7[1, 1] = array2[1, 1] + Math.Cos(angle) * array6[1, 1] + Math.Sin(angle) * array4[1, 1];
			array7[1, 2] = array2[1, 2] + Math.Cos(angle) * array6[1, 2] + Math.Sin(angle) * array4[1, 2];
			array7[2, 0] = array2[2, 0] + Math.Cos(angle) * array6[2, 0] + Math.Sin(angle) * array4[2, 0];
			array7[2, 1] = array2[2, 1] + Math.Cos(angle) * array6[2, 1] + Math.Sin(angle) * array4[2, 1];
			array7[2, 2] = array2[2, 2] + Math.Cos(angle) * array6[2, 2] + Math.Sin(angle) * array4[2, 2];
			double[,] array8 = array7;
			return new XYZ(array8[0, 0] * vect.X + array8[0, 1] * vect.Y + array8[0, 2] * vect.Z, array8[1, 0] * vect.X + array8[1, 1] * vect.Y + array8[1, 2] * vect.Z, array8[2, 0] * vect.X + array8[2, 1] * vect.Y + array8[2, 2] * vect.Z);
		}

		public static bool AssignParameters(Document doc, Wire wire)
		{
			bool result;
			try
			{
				wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_TICKMARK_STATE).Set(2);
                //wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_TYPE).Set(WireConnectForm.wireParams.WireStyle);
				wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_HOT_ADJUSTMENT).Set(WireConnectForm.wireParams.WireNum);
				wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_GROUND_ADJUSTMENT).Set(1);
				wire.get_Parameter(BuiltInParameter.RBS_ELEC_WIRE_NEUTRAL_ADJUSTMENT).Set(1);
				NewConnectWire.AddSharedParamsForWire(doc, wire);
				result = true;
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static void AddSharedParamsForWire(Document doc, Wire wire)
		{
			CmdSetValueToWire.AddSharedParamsToWire(doc, wire);
		}

		private static bool IsHasFile()
		{
			return File.Exists(NewConnectWire.path);
		}

		public static void LoadDlgData(Document doc)
		{
			List<RevitWireType> revitWireTypes = ElectricSettingDataAssist.GetRevitWireTypes(doc);
			List<RevitConduitType> revitConduitTypes = ElectricSettingDataAssist.GetRevitConduitTypes(doc);
			NewConnectWire.xmlHelper.LoadXml(NewConnectWire.path, "Root");
			NewConnectWire.xmlHelper.SelectNode(Path.Combine("Root", "HYConnectWire", "Params"));
			string attribute = NewConnectWire.xmlHelper.GetAttribute("WireType");
			if (!string.IsNullOrEmpty(attribute))
			{
				using (List<RevitWireType>.Enumerator enumerator = revitWireTypes.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.Name == attribute)
						{
							WireConnectForm.wireParams.WireType = attribute;
							break;
						}
					}
				}
			}
			if (!string.IsNullOrEmpty(NewConnectWire.xmlHelper.GetAttribute("WireNum")))
			{
				WireConnectForm.wireParams.WireNum = Convert.ToInt32(NewConnectWire.xmlHelper.GetAttribute("WireNum"));
			}
			string attribute2 = NewConnectWire.xmlHelper.GetAttribute("CrossSection");
			if (!string.IsNullOrEmpty(attribute2))
			{
				WireConnectForm.wireParams.CrossSection = Convert.ToDouble(attribute2);
			}
			string attribute3 = NewConnectWire.xmlHelper.GetAttribute("LayMode");
			if (!string.IsNullOrEmpty(attribute3))
			{
				WireConnectForm.wireParams.LayMode = attribute3;
			}
			string attribute4 = NewConnectWire.xmlHelper.GetAttribute("Level");
			if (!string.IsNullOrEmpty(attribute4))
			{
				WireConnectForm.wireParams.Level = Convert.ToDouble(attribute4);
			}
			string attribute5 = NewConnectWire.xmlHelper.GetAttribute("ConduitTypeID");
			if (!string.IsNullOrEmpty(attribute5))
			{
				foreach (RevitConduitType revitConduitType in revitConduitTypes)
				{
					if (revitConduitType.ConduitTypeId.ToString() == attribute5)
					{
						WireConnectForm.wireParams.ConduitTypeID = revitConduitType.ConduitTypeId;
						WireConnectForm.wireParams.ConduitType = revitConduitType.Name;
					}
				}
			}
			string attribute6 = NewConnectWire.xmlHelper.GetAttribute("ConduitDiameter");
			if (!string.IsNullOrEmpty(attribute6))
			{
				WireConnectForm.wireParams.ConduitDiameter = Convert.ToDouble(attribute6);
			}
		}

		private static YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private static string path = Path.Combine(Product.WorkingLocation, "ConnectWire.xml");

		private const string Root = "Root";

		private const string Node = "HYConnectWire";

		private const string ChildNode = "Params";

		private const string WireType = "WireType";

		private const string WireNum = "WireNum";

		private const string CrossSection = "CrossSection";

		private const string LayMode = "LayMode";

		private const string Level = "Level";

		private const string ConduitDiameter = "ConduitDiameter";

		private const string ConduitTypeID = "ConduitTypeID";

		private static List<Curve> allCurves = new List<Curve>();

		private static List<XYZ> allPoints = new List<XYZ>();

		public static bool isFirstEquipment = false;
	}
}
